home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: embedded strtok() calls on different strings?
- Date: 8 Feb 1996 11:05:06 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4fdhh2$1bs@crl.crl.com>
- References: <DMGrn7.1Bx0@CompStar.bnr.ca>
- NNTP-Posting-Host: crl.com
-
- Sorry, but you're stuck. strtok() is acting exactly as defined, modifying
- string1 and string2 (and you're lucky you're on a system that didn't
- write-protect the strings), and losing the pointer to string1. The only
- way around this is to write your own version of strtok that is able to
- handle multiple sets of strings. It's not too difficult, but your
- function will then work differently than the standard strtok().
-
- Bob
-
- tzinck@bnr.ca writes:
-
- >char *string1="This is string one";
- >char *string2="This is string two";
-
- >void *vp1;
- >void *vp2;
-
- >vp1 = strtok(string1," ");
-
- >while (vp1 != NULL){
- > printf("Tok1 = %s\n", (char *) vp1);
-
- > vp2 = strtok(string2," ");
- > while (vp2!=NULL){
- > printf("Tok2 = %s\n", (char *) vp2);
- > vp2 = strtok(NULL, " ");
- > }
-
- > vp1 = strtok(NULL, " ");
- >}
-
- >But the output is :
- >Tok1 = This
- >Tok2 = This
- >Tok2 = is
- >Tok2 = string
- >Tok2 = two
-
-
- >as vp1 gets corrupted. Any thoughts ?
-
- Both string1 and stringtwo should be modified by strtok (with NULLs
- inserted between your tokens). Calling strtok after the last token has
- been read is an error, and undefined (it crashes on my system). This is
- what would happen on my system if I tried to run your code and reached
- the final call.
-
- Bob
-